package com.wollo.controller;

import com.github.pagehelper.PageInfo;
import com.wollo.domain.AjaxResult;
import com.wollo.domain.Check;
import com.wollo.dto.StudentCheckWorkDTO;
import com.wollo.dto.StudentPageDTO;
import com.wollo.service.StudentCheckWorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

/**
 * @author 栾钰凇
 * Date  2024/9/13 21:43
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentCheckWorkController {
    @Autowired
    private StudentCheckWorkService studentCheckWorkService;

    /**
     * 获取当前登录的用户信息
     *
     * @return
     */
    @GetMapping("/userInfo")
    public AjaxResult userInfo() {
        log.info("获取当前登录的用户信息");
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            return AjaxResult.ok(user);
        } else {
            return AjaxResult.error();
        }
    }

    @PostMapping("/getPage")
    public AjaxResult getPage(@RequestBody StudentPageDTO studentPageDTO) {
        log.info("学生分页查询:{}", studentPageDTO);
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        studentPageDTO.setUsername(user.getUsername());
        PageInfo<Check> pageInfo = studentCheckWorkService.getAllCheckInfo(studentPageDTO);
        if (pageInfo == null || pageInfo.getSize() == 0) {
            return AjaxResult.error();
        } else {
            return AjaxResult.ok(pageInfo);
        }
    }

    /**
     * 打卡
     *
     * @return
     */
    @PutMapping("/checkWork")
    public AjaxResult checkWork(@RequestBody StudentCheckWorkDTO studentCheckWorkDTO) {
        log.info("学生打卡:{}", studentCheckWorkDTO);
        boolean checkResult = studentCheckWorkService.addCheckInfo(studentCheckWorkDTO);
        if (checkResult){
            return AjaxResult.ok("打卡成功");
        }else {
            return AjaxResult.error();
        }

    }

}
